<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:p="pilferpage"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
	xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
	xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
	xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
	xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
	xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
	xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
	xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
	xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
	xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
	xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
	xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
	>


	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

	<xsl:template match="/p:page">
		<office:document-content office:version="1.1">
			<office:scripts/>
			<office:font-face-decls>
				<style:font-face style:name="Nimbus Sans L" svg:font-family="'Nimbus Sans L'" style:font-family-generic="swiss" style:font-pitch="variable"/>
				<style:font-face style:name="DejaVu Sans" svg:font-family="'DejaVu Sans'" style:font-family-generic="system" style:font-pitch="variable"/>
			</office:font-face-decls>
			<office:automatic-styles>
				<style:style style:name="columnStyle" style:family="table-column">
					<style:table-column-properties fo:break-before="auto" style:column-width="2.267cm"/>
				</style:style>
				<style:style style:name="rowStyle" style:family="table-row">
					<style:table-row-properties fo:break-before="auto" style:use-optimal-row-height="true"/>
				</style:style>
			</office:automatic-styles>
			<office:body>
				<office:spreadsheet>
					<xsl:apply-templates select="//p:table"/>
				</office:spreadsheet>
			</office:body>
		</office:document-content>
	</xsl:template>


	<xsl:template match="p:title">
		<text:h text:outline-level="{count(ancestor::p:section)+1}">
			<xsl:apply-templates/>
		</text:h>
	</xsl:template>

	<xsl:template match="p:paragraph">
		<xsl:element name="text:p">
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:table">
		<xsl:variable name="tableId">
			<xsl:text>Table</xsl:text>
		</xsl:variable>
			<xsl:number/>
		<table:table table:style-name="{$tableId}">
			<xsl:attribute name="table:name">
				<xsl:choose>
					<xsl:when test="p:title"><xsl:value-of select="p:title"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="$tableId"/></xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="p:head/p:row[1]/p:cell">
					<xsl:for-each select="p:head/p:row[1]/p:cell">
						<table:table-column table:style-name="columnStyle"/>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="p:row[1]/p:cell | p:body/p:row[1]/p:cell">
						<table:table-column table:style-name="columnStyle"/>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="p:head"/>
			<xsl:apply-templates select="p:row | p:body"/>
			<xsl:apply-templates select="p:foot"/>
		</table:table>
	</xsl:template>

	<xsl:template match="p:row">
		<table:table-row table:style-name="rowStyle">
			<xsl:apply-templates select="p:cell"/>
		</table:table-row>
		<xsl:apply-templates select="p:row"/>
	</xsl:template>

	<xsl:template match="p:cell">
		<xsl:variable name="innerText">
			<xsl:for-each select="text()">
				<xsl:value-of select="."/>
			</xsl:for-each>
		</xsl:variable>
		<table:table-cell office:value-type="string">
			<xsl:choose>
				<xsl:when test="normalize-space($innerText)">
					<xsl:element name="text:p">
						<xsl:value-of select="normalize-space(text())"/>
					</xsl:element>
					<xsl:apply-templates select="*"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
		</table:table-cell>
	</xsl:template>

	<xsl:template match="p:link">
		<xsl:element name="text:a">
			<xsl:attribute name="xlink:href"><xsl:value-of select="@url"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>
